﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media.Animation;
using System.Windows;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Text.RegularExpressions;
using System.Threading;

namespace Hugo
{
    public abstract class UIPage : UIOption
    {
        
        public MainWindow MainWindow { get; set;  }
        public Canvas ParentCanvas { get; set; }
        public Canvas PageCanvas { get; set; }
        private List<String> AcceptedTerms; 

        static int nextPageCanvasID = 0;

        public bool isBackButtonVisible { get; set; }
        public bool isHomeButtonVisible { get; set; }
        public bool isNurseButtonVisible { get; set; }
        public bool areQuickButtonsVisible { get; set; }
        public bool areShiftButtonsVisible { get; set; }

        public bool overridesHandCursor = false;

        public UIPage(String name, BitmapSource iconSource) : base(name,iconSource)
        {
            this.MainWindow = MainWindow.getInstance();
            //this.Name = name;
            this.IconSource = iconSource;
            this.ParentCanvas = this.MainWindow.pageParentCanvas;
            PageCanvas = new Canvas();
            PageCanvas.Name = "pagecanvas" + (nextPageCanvasID++);
            this.MainWindow.RegisterName(PageCanvas.Name, PageCanvas);
            ParentCanvas.Children.Add(PageCanvas);
            PageCanvas.Opacity = 0;

            this.areShiftButtonsVisible = true;
            this.areQuickButtonsVisible = true;
            this.isHomeButtonVisible = true;
            this.isNurseButtonVisible = true;
            this.isBackButtonVisible = true;

            AcceptedTerms = new List<string>();
            if (areShiftButtonsVisible) { AcceptedTerms.Add("Left"); AcceptedTerms.Add("Right"); }
            if (isHomeButtonVisible) { AcceptedTerms.Add("Home"); }
            if (isNurseButtonVisible) { AcceptedTerms.Add("Nurse"); }
            //AcceptedTerms.Add("Inbox");
            AcceptedTerms.Add("Back");
            AcceptedTerms.Add("Select");
        }

        public void addAcceptedVoiceTerm(String term)
        {
            AcceptedTerms.Add(term);
        }

        public override void wasSelected() {
            MainWindow.pushPage(this);
        }

        public virtual void setPage()
        {
            Thread Messagethread = new Thread(
                    new ThreadStart(delegate()
                    {
                        MainWindow.getInstance().Dispatcher.Invoke(
                          System.Windows.Threading.DispatcherPriority.Normal,
                          new Action(
                            delegate()
                            {
            DoubleAnimation myDoubleAnimation = AnimationFactory.getFadeInAnimation();

            Storyboard myStoryboard = new Storyboard();
            myStoryboard.Children.Add(myDoubleAnimation);
            Storyboard.SetTargetName(myDoubleAnimation, PageCanvas.Name);
            Storyboard.SetTargetProperty(myDoubleAnimation, new PropertyPath(Canvas.OpacityProperty));

            myStoryboard.Begin(this.MainWindow);

            NavigationPage.getInstance().setShiftNavigationsButtonVisible(this.areShiftButtonsVisible);
            NavigationPage.getInstance().setQuickNavigationButtonsVisible(this.areQuickButtonsVisible);
            NavigationPage.getInstance().setBackNavigationButtonsVisible(this.isBackButtonVisible);
            NavigationPage.getInstance().setHomeNavigationButtonVisible(this.isHomeButtonVisible && areQuickButtonsVisible);
            NavigationPage.getInstance().setNurseNavigationButtonVisible(this.isNurseButtonVisible && areQuickButtonsVisible);
            NavigationPage.getInstance().updateButtonVisibility();

            if (GlobalSettings.isUsingKinect && GlobalSettings.isUsingVoice)
            {
                Voice.getInstance().setAcceptableTerms(AcceptedTerms);
            }
                            }
                        ));
                    }));
            Messagethread.SetApartmentState(ApartmentState.STA);
            Messagethread.Start();

        }

        public virtual void respondToRecognizedVoiceCommand(String command)
        {
            MainWindow.getInstance().restartSleepTimer();
            if (command.Equals("Home")) { MainWindow.popAllPages(); }
            else if (command.Equals("Nurse")) { MainWindow.callNurse(); }
            else if (command.Equals("Left")) { MainWindow.getCurrentPage().shiftLeft(); }
            else if (command.Equals("Right")) { MainWindow.getCurrentPage().shiftRight(); }
            //else if (command.Equals("Inbox")) { MainWindow.showInbox(); }
            else if (command.Equals("Back")) { MainWindow.popPage();  }
            else if (command.Equals("Select")) { MainWindow.getCurrentPage().select(); }
        }

        public virtual bool respondToKey(KeyEventArgs e) { return false; }
        public virtual bool respondToClick(MouseButtonEventArgs e) { return false; }

        public virtual void shiftLeft() { }
        public virtual void shiftRight() { }
        public virtual void select() { }
        
        public virtual bool setCursorPosition(Point p)
        {
            return false;
        }

        public virtual void unSetPage()
        {
            Thread Messagethread = new Thread(
                    new ThreadStart(delegate()
                    {
                        MainWindow.getInstance().Dispatcher.Invoke(
                          System.Windows.Threading.DispatcherPriority.Normal,
                          new Action(
                            delegate()
                            {
                                DoubleAnimation myDoubleAnimation = AnimationFactory.getFadeOutAnimation();

                                Storyboard myStoryboard = new Storyboard();
                                myStoryboard.Children.Add(myDoubleAnimation);
                                Storyboard.SetTargetName(myDoubleAnimation, PageCanvas.Name);
                                Storyboard.SetTargetProperty(myDoubleAnimation, new PropertyPath(Canvas.OpacityProperty));

                                myStoryboard.Begin(this.MainWindow);
                            }
                        ));
                    }));
            Messagethread.SetApartmentState(ApartmentState.STA);
            Messagethread.Start();
            
        }

        internal void silentUnset()
        {
            //PageCanvas.Opacity = 0;
        }
    }
}
